
let server_url = ''
if (process.env.NODE_ENV === 'development') {
  server_url = 'http://localhost:8080'; //测试环境
} else {
  server_url = 'https://lucaizhu.lupuamc.com:9888'; //生产环境
}

let ajaxTimes = 0;  //同时发送异步代码的次数,防止一个页面或一个操作中有多次请求处理
function service(options = {}) {
	ajaxTimes++;
	if(options.isUseMock){
		options.url = options.url;
	}else{
		options.url = server_url + options.url;
	}
	options.method = options.method || 'GET';
	options.data = options.params
	options.header = {'content-type': 'application/json'};
	let token = uni.getStorageSync('TOKEN');
	if(token){
		options.header.Authorization = 'Bearer ' + token
	}
	uni.showLoading({
		title: "加载中",
		mask: true,
	});
	return new Promise((resolved, rejected) => {
		ajaxTimes--;
		if (ajaxTimes === 0) {
			uni.hideLoading()
		}
			//成功
		options.success = (res) => {
			if (res.statusCode == 200) {  
				resolved(res.data);
			} else {
				uni.showToast({
					icon: 'none',
					duration: 3000,
					title: '网络开小差，请稍后再试'
				});
				rejected(res.data.message); 
			}
		}
			//错误
		options.fail = (err) => {
			rejected(err); //错误
		}
		uni.request(options);
	}).catch((e) => {
		uni.hideLoading();
		console.log(e)
	})
}
export default service;

